package example.websocket;

import java.util.LinkedList;

public class MeetingItemCantainer {
    private static  MeetingItemCantainer mic = null;
    LinkedList<MeetingItem> ll = new LinkedList<MeetingItem>();
//    HashMap<String,MeetingItem> hm = new HashMap<String,MeetingItem>();
    int currentindex = -1;
    private MeetingItemCantainer()
    {

    }
    public static MeetingItemCantainer getInstance()
    {
        if (mic == null)
        {
            mic  = new MeetingItemCantainer();

            MeetingItem mi1 = new MeetingItem();

            MeetingItem mi2 = new MeetingItem();
            MeetingItem mi3 = new MeetingItem();

            mi1.setName("John");
            mi1.setContents("contents 1");
            mi2.setName("zgw2");
            mi2.setContents("contents 2");
            mi3.setName("zgw3");
            mi3.setContents("contents 3");
            MeetingItemCantainer.getInstance().addItem(mi1);
            MeetingItemCantainer.getInstance().addItem(mi2);
            MeetingItemCantainer.getInstance().addItem(mi3);


            for (int i = 0; i<2000;i++)
            {
                MeetingItem mi_t = new MeetingItem();
                mi_t.setName("name"+i);
                mi_t.setContents("contents"+i);
                MeetingItemCantainer.getInstance().addItem(mi_t);
            }

        }
        return mic;
    }

    public MeetingItem getCurrentItem()
    {

        return this.ll.get(currentindex);

    }
    //获得前一个
    public MeetingItem getPreItem()
    {
        if(currentindex >0)
            currentindex--;
        else
            currentindex = 0;
        return getCurrentItem();
    }


    public MeetingItem getNextItem()
    {

        if(currentindex>= this.ll.size()-1)
        {
            return this.ll.get(currentindex);
        }

        return this.ll.get(++currentindex);

    }
    public void addItem(MeetingItem mi)
    {
        if (!ll.contains(mi))
        {
            ll.add(mi);
        }
//        hm.put(mi.name,mi);
    }

    public void remove(MeetingItem mi)
    {
        if(ll.contains(mi))
            ll.remove(mi);
//        hm.remove(mi.name);
    }

    public MeetingItem getMeetingItem(String id)
    {

        for (MeetingItem mi:ll)
        {
            if(mi.name.equals(id))
                return mi;
        }
        return null;
//        return  hm.get(id);
    }

    public static void main(String[] argc)
    {

        String tempDir = System.getProperty("java.io.tmpdir");
        System.out.println("Temporary directory: " + tempDir);

        MeetingItem mi1 = new MeetingItem();

        MeetingItem mi2 = new MeetingItem();
        MeetingItem mi3 = new MeetingItem();

        mi1.setName("1");
        mi2.setName("2");
        mi3.setName("3");
        MeetingItemCantainer.getInstance().addItem(mi1);
        MeetingItemCantainer.getInstance().addItem(mi2);
        MeetingItemCantainer.getInstance().addItem(mi3);
        System.out.println(MeetingItemCantainer.getInstance().getMeetingItem("John"));
        System.out.println(MeetingItemCantainer.getInstance().getCurrentItem());
        System.out.println(MeetingItemCantainer.getInstance().getNextItem());
        System.out.println(MeetingItemCantainer.getInstance().getNextItem());

//        System.out.println(MeetingItemCantainer.getInstance().hm);
    }
}
